package com.magic233.service;

import com.magic233.entity.*;
import com.magic233.mapper.OrdersMapper;
import com.magic233.mapper.WorkTypeMapper;
import com.magic233.mapper.WorkersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * ClassName: WorkersServiceImpl
 * Description:
 * date: 2021/5/30 0:33
 * author: zouyuan
 */
@Service
public class WorkersServiceImpl implements WorkersService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private LogsService logsService;

    @Autowired
    private WorkersMapper workersMapper;

    @Autowired
    private OrdersMapper ordersMapper;

    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    @Override
    public int addWorker(Users user, Long wid, String type) {
        Workers worker = workersMapper.selectByPrimaryKey2(wid);
        if (worker == null) {
            //工人不存在workers表
            worker = new Workers();
            worker.setId(wid);
            worker.setType(type);
            worker.setDeleted(0);
            worker.setAcceptnum(0);
            worker.setCompletenum(0);

            int re = workersMapper.insert(worker);

            if (re == 1) {
                String thing = "超级管理员：{" + user.getPhone() + "--" + user.getUsername() + "}新增了维修人员：" + wid;
                logsService.addLog(thing);
            }
            return re;
        } else if (worker.getDeleted() == 1) {
            //工人存在workers表,但已被逻辑删除
            worker = new Workers();
            worker.setId(wid);
            worker.setType(type);
            worker.setDeleted(0);

            int re = workersMapper.updateByPrimaryKeySelective(worker);

            if (re == 1) {
                String thing = "超级管理员：{" + user.getPhone() + "--" + user.getUsername() + "}新增了维修人员：" + wid;
                logsService.addLog(thing);
            }
            return re;
        } else {
            return 0;
        }
    }

    @Override
    public List<Workers> selectWorkers() {
        List<Workers> workers = workersMapper.selectWorkers();
        return workers;
    }

    @Override
    @Transactional
    public int deleteWorker(Users user, Long id) {
        //将该工人除开已完成、工人已完成状态的工单设置为未派单，由管理员重新派单
        Orders orders = new Orders();
        Long time = new Date().getTime();
        String updatetime = simpleDateFormat.format(time);
        orders.setWid(id);
        orders.setUpdatetime(updatetime);
        int re = ordersMapper.updateOrderStatusByWorkerDisable(orders);
        System.out.println("有" + re + "条工单被重置为未派单");
        //从维修工人表中将该用户逻辑删除。
        Workers worker = new Workers();
        worker.setId(id);
        worker.setDeleted(1);
        int re2 = workersMapper.updateByPrimaryKeySelective(worker);
        //如果该账号在线，进行强制下线
        Set<String> keys = redisTemplate.keys("*");
        for (String key : keys) {
            System.out.println(key);
            Users oldUser = null;
            try {
                oldUser = (Users) redisTemplate.opsForValue().get(key);
                if (id.equals(oldUser.getId())) {
                    System.out.println("账号已经登录，强制下线");
                    //将别处登录的账号key删除，即踢下线
                    redisTemplate.delete(key);
                    break;
                }
            } catch (Exception e) {
//                        e.printStackTrace();
                System.out.println("转换失败" + key);
            }
        }
        if (re2 == 1) {
            String thing = "超级管理员：{" + user.getPhone() + "--" + user.getUsername() + "}删除了维修人员：" + id;
            logsService.addLog(thing);
        }
        return re2;
    }
}
